package com.gaogzhen.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaogzhen.domain.TradeArea;
import com.gaogzhen.dto.TradeAreaDto;
import com.gaogzhen.feign.TradingAreaServiceClient;
import com.gaogzhen.service.TradeAreaService;
import com.gaogzhen.utils.CurrentUser;
import com.gaogzhen.utils.R;
import com.gaogzhen.utils.SqlExecuteUtils;
import com.gaogzhen.vo.TradeAreaMarketsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author Administrator
 * @date 2022-12-08 10:37
 */
@RestController
@RequestMapping("/tradeAreas")
@Api(tags = "交易区域的数据接口")
public class TradeAreaController implements TradingAreaServiceClient {

    @Autowired
    private TradeAreaService tradeAreaService;

    @GetMapping
    @ApiOperation("分页查询交易区域列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "交易区域的名称"),
            @ApiImplicitParam(name = "status", value = "状态"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页大小")
    })
    @PreAuthorize("hasAuthority('trade_area_query')")
    public R<Page<TradeArea>> findByPage(@ApiIgnore Page<TradeArea> page, String name, Byte status) {
        return R.ok(tradeAreaService.findByPage(page, name, status));

    }

    @GetMapping("/all")
    @ApiOperation("查询交易区域列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "状态")
    })
    @PreAuthorize("hasAuthority('trade_area_query')")
    public R<List<TradeArea>> findByPage(Byte status) {
        return R.ok(tradeAreaService.findAll(status));

    }

    @PostMapping
    @ApiOperation("新增交易区域")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tradeArea", value = "tradeArea json数据")
    })
    public R add(@RequestBody @Validated TradeArea tradeArea) {
        SqlExecuteUtils.sqlExceptionHandler(a -> tradeAreaService.save(a), tradeArea, "新增交易区域失败");
        return R.ok();
    }

    @PatchMapping
    @ApiOperation("修改交易区域")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tradeArea", value = "tradeArea json数据")
    })
    public R update(@RequestBody @Validated TradeArea tradeArea) {
        SqlExecuteUtils.sqlExceptionHandler(a -> tradeAreaService.updateById(a), tradeArea, "修改交易区域失败");
        return R.ok();
    }

    @PostMapping("/status")
    @ApiOperation("修改交易区域状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "交易区域id"),
            @ApiImplicitParam(name = "status", value = "状态值")
    })
    public R updateStatus(@NotNull Long id, @NotNull Byte status) {
        tradeAreaService.updateStatus(id, status);
        return R.ok();
    }

    @GetMapping("/markets")
    @ApiOperation("查询所有交易区域及交易区域下的交易市场")
    public R<List<TradeAreaMarketsVo>> getTradeAreaMarkets() {
        return R.ok(tradeAreaService.getTradeAreaMarkets());
    }

    @GetMapping("/market/favorite")
    @ApiOperation("查询用户收藏交易区域及交易区域下的交易市场")
    public R<List<TradeAreaMarketsVo>> getUserFavoriteMarkets() {
        return R.ok(tradeAreaService.getUserFavoriteMarkets(CurrentUser.getCurrentUserId()));
    }

    /**
     * 查询所有的交易区域
     *
     * @return
     */
    @Override
    public List<TradeAreaDto> tradeAreas() {
        List<TradeAreaDto> tradeAreaDtoList =  tradeAreaService.findAllTradeAreaAndMarket();
        return tradeAreaDtoList ;
    }
}
